package com.augurit.wf.client.summary.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.wf.client.summary.service.IWfTaskSummaryService;
import com.augurit.wf.client.summary.web.form.WfTaskSummaryForm;
import com.augurit.wf.common.extmodel.SummaryCountNode;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "wf-task-summary.action", type = "redirect"),
	@Result(name = WfTaskSummaryAction.PAGE_DB_SUMMARY, location = "/wf/client/summary/db-summary.jsp"),
	@Result(name = WfTaskSummaryAction.PAGE_ZB_SUMMARY, location = "/wf/client/summary/zb-summary.jsp"),
	@Result(name = WfTaskSummaryAction.PAGE_DZB_SUMMARY, location = "/wf/client/summary/dzb-summary.jsp"),
	@Result(name = WfTaskSummaryAction.PAGE_YB_SUMMARY, location = "/wf/client/summary/yb-summary.jsp"),
	@Result(name = WfTaskSummaryAction.PAGE_BJ_SUMMARY, location = "/wf/client/summary/bj-summary.jsp"),
	@Result(name = WfTaskSummaryAction.PAGE_DB_SUMMARY_WIDGET, location = "/wf/client/summary/db-summary-widget.jsp"),
	@Result(name = WfTaskSummaryAction.PAGE_ZB_SUMMARY_WIDGET, location = "/wf/client/summary/zb-summary-widget.jsp"),
	@Result(name = WfTaskSummaryAction.PAGE_DZB_SUMMARY_WIDGET, location = "/wf/client/summary/dzb-summary-widget.jsp"),
	@Result(name = WfTaskSummaryAction.PAGE_SUMMARY_COUNT_WIDGET, location = "/wf/client/summary/summary-count-widget.jsp")
})
public class WfTaskSummaryAction extends ExtBaseAction implements ModelDriven<WfTaskSummaryForm>, Preparable {

	public static final String PAGE_DB_SUMMARY = "PAGE_DB_SUMMARY";
	public static final String PAGE_ZB_SUMMARY = "PAGE_ZB_SUMMARY";
	public static final String PAGE_DZB_SUMMARY = "PAGE_DZB_SUMMARY";
	public static final String PAGE_YB_SUMMARY = "PAGE_YB_SUMMARY";
	public static final String PAGE_BJ_SUMMARY = "PAGE_BJ_SUMMARY";
	public static final String PAGE_DB_SUMMARY_WIDGET = "PAGE_DB_SUMMARY_WIDGET";
	public static final String PAGE_ZB_SUMMARY_WIDGET = "PAGE_ZB_SUMMARY_WIDGET";
	public static final String PAGE_DZB_SUMMARY_WIDGET = "PAGE_DB_AND_ZB_SUMMARY_WIDGET";
	public static final String PAGE_SUMMARY_COUNT_WIDGET = "PAGE_SUMMARY_COUNT_WIDGET";
	
	@Autowired
	private IWfTaskSummaryService wfTaskSummaryService;

	
	private WfTaskSummaryForm form;
	private Long id;
	private Long[] checkedIds;
	private Page<WfTaskSummaryForm> page = new Page<WfTaskSummaryForm>(10);

	private String groupBy;
	private String groupDir;
	
	
	public String loadDbSummaryView() throws Exception {
		return PAGE_DB_SUMMARY;
	}
	
	public String loadZbSummaryView() throws Exception {
		return PAGE_ZB_SUMMARY;
	}
	
	public String loadDZbSummaryView() throws Exception {
		return PAGE_DZB_SUMMARY;
	}
	
	public String loadYbSummaryView() throws Exception {
		return PAGE_YB_SUMMARY;
	}
	
	public String loadBjSummaryView() throws Exception {
		return PAGE_BJ_SUMMARY;
	}
	
	public String loadDbSummaryWidget() throws Exception {
		return PAGE_DB_SUMMARY_WIDGET;
	}
	
	public String loadZbSummaryWidget() throws Exception {
		return PAGE_ZB_SUMMARY_WIDGET;
	}
	
	public String loadDZbSummaryWidget() throws Exception {
		return PAGE_DZB_SUMMARY_WIDGET;
	}
	
	public String loadSummaryCountWidget() throws Exception {
		return PAGE_SUMMARY_COUNT_WIDGET;
	}
	
	public String getDbSummary() throws Exception {
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		wfTaskSummaryService.getDbSummary(page, form, this.getLoginUserName(), this.getGroupBy(), this.getGroupDir());
		renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	public String getZbSummary() throws Exception {
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		wfTaskSummaryService.getZbSummary(page, form, this.getLoginUserName(), this.getGroupBy(), this.getGroupDir());
		renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	public String getDZbSummary() throws Exception {
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		wfTaskSummaryService.getDbAndZbSummary(page, form, this.getLoginUserName(), this.getGroupBy(), this.getGroupDir());
		renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	public String getYbSummary() throws Exception {
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		wfTaskSummaryService.getYbSummary(page, form, this.getLoginUserName(), this.getGroupBy(), this.getGroupDir());
		renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}

	public String getBjSummary() throws Exception {
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		wfTaskSummaryService.getBjSummary(page, form, this.getLoginUserName(), this.getGroupBy(), this.getGroupDir());
		renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	public String getSummaryCount() throws Exception {
		List<SummaryCountNode> children = wfTaskSummaryService.getSummaryCount(this.getLoginUserName(), form);
		ExtBaseAction.extRenderListJson(children, false);
		return null;
	}
	
	public void prepare() throws Exception {
		prepareModel();
	}

	protected void prepareModel() throws Exception {
		form = new WfTaskSummaryForm();
	}
	

	public WfTaskSummaryForm getModel() {
		return form;
	}
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(Long[] checkedIds) {
		this.checkedIds = checkedIds;
	}
	
	public Page<WfTaskSummaryForm> getPage() {
		return page;
	}

	public void setPage(Page<WfTaskSummaryForm> page) {
		this.page = page;
	}

	public String getGroupBy() {
		return groupBy;
	}

	public void setGroupBy(String groupBy) {
		this.groupBy = groupBy;
	}

	public String getGroupDir() {
		return groupDir;
	}

	public void setGroupDir(String groupDir) {
		this.groupDir = groupDir;
	}
}
